#include "asserts.hpp"
#include "foreach.hpp"
#include "formatter.hpp"
#include "grid_widget.hpp"
#include "image_widget.hpp"
#include "label.hpp"
#include "player.hpp"
#include "raster.hpp"
#include "scenario.hpp"
#include "scenario_complete_screen.hpp"
#include "scenario_setup_screen.hpp"
#include "texture.hpp"
#include "unit_utils.hpp"

namespace game {

scenario_complete_screen::scenario_complete_screen(scenario& s, ScenarioCarryoverState& state)
  : gui::dialog(0, 0, graphics::screen_width(), graphics::screen_height()),
    scenario_(s), state_(state),
    base_gold_(scenario_.base_gold_reward()),
    base_experience_(scenario_.base_experience_reward()),
    bonus_gold_((scenario_.bonus_gold_reward()*scenario_.performance_rating())/100),
    bonus_experience_((scenario_.bonus_experience_reward()*scenario_.performance_rating())/100),
    wait_at_start_(50), done_(false)
{
	ASSERT_EQ(scenario_.players().empty(), false);

	int nsurvivors = 0;
	foreach(commander_unit_ptr cmd, scenario_.players().front()->commanders()) {
		nsurvivors += cmd->soldiers().size();
	}

	surviving_units_refund_ = nsurvivors*5;

	init();
}

void scenario_complete_screen::process()
{
	init();
	if(wait_at_start_ > 0) {
		--wait_at_start_;
		return;
	}

	if(base_experience_ > 0) {
		--base_experience_;
		foreach(commander_unit_ptr cmd, scenario_.players().front()->commanders()) {
			cmd->award_experience();
		}
		return;
	}

	if(base_gold_ > 0) {
		--base_gold_;
		++state_.gold;
		return;
	}

	if(bonus_gold_ > 0) {
		--bonus_gold_;
		++state_.gold;
		return;
	}

	if(surviving_units_refund_ > 0) {
		--surviving_units_refund_;
		++state_.gold;
		return;
	}

	if(bonus_experience_ > 0) {
		--bonus_experience_;
		++state_.experience;
		return;
	}

	done_ = true;
}

void scenario_complete_screen::init()
{
	clear();

	SDL_Color color = graphics::color_white();
	using namespace gui;
	add_widget(label::create("Scenario Complete", color, 48), 10, 10);

	grid_ptr g(new grid(3));
	g->set_hpad(10);

	g->add_col(label::create("", color))
	  .add_col(label::create("Gold", color, 16))
	  .add_col(label::create("Experience", color, 16));
	g->add_col(label::create("Scenario Clearance", color, 16))
	  .add_col(label::create(formatter() << base_gold_, color, 16))
	  .add_col(label::create(formatter() << base_experience_, color, 16));
	g->add_col(label::create(formatter() << "Bonus (" << scenario_.performance_rating() << "%)", color, 16))
	  .add_col(label::create(formatter() << bonus_gold_, color, 16))
	  .add_col(label::create(formatter() << bonus_experience_, color, 16));
	g->add_col(label::create("Surviving Units", color, 16))
	  .add_col(label::create(formatter() << surviving_units_refund_, color, 16))
	  .add_col(label::create("-", color, 16));

	add_widget(g);

	g = grid_ptr(new grid(6));
	g->set_hpad(10);
	foreach(commander_unit_ptr cmd, scenario_.players().front()->commanders()) {
		g->add_col(widget_ptr(new image_widget(get_unit_image(*cmd))))
		  .add_col(label::create(cmd->description(), color, 16))
		  .add_col(label::create(formatter() << "Level " << (cmd->level()+1), color, 16))
		  .add_col(label::create(cmd->type().description(), color, 16))
		  .add_col(label::create(formatter() << cmd->experience(), color, 16))
		  .add_col(label::create(formatter() << "/" << cmd->experience_required(), color, 16));
	}

	add_widget(g);

	g = grid_ptr(new grid(2));
	g->set_hpad(10);
	g->add_col(label::create("Gold", color, 40))
	  .add_col(label::create(formatter() << state_.gold, color, 40))
	  .add_col(label::create("Experience", color, 40))
	  .add_col(label::create(formatter() << state_.experience, color, 40));
	add_widget(g, graphics::screen_width() - g->width() - 20,
	              graphics::screen_height() - g->height() - 20);
}

}
